package vip.xiaomoli.courier.dao.sql;

import vip.xiaomoli.courier.util.CourierUtil;

import java.util.List;

public class Sql_MySql implements Sql {

    @Override
    public String INSERT_TASK_INFO() {
        return "INSERT INTO courier_task_info(courierName,param,execTime,loadFlag,loadTime,execServerIP) " +
                "VALUES(?,?,?,?,?,?)";
    }

    @Override
    public String RECORD_EXEC_FAIL() {
        return "UPDATE courier_task_info SET failMessage=?,failTimes=?," +
                "execBgnTime=?,execEndTime=?,execMillis=?,loadFlag=0,loadTime=null " +
                "WHERE id=?";
    }

    @Override
    public String RECORD_EXEC_SUCCESS() {
        return "INSERT IGNORE INTO courier_task_info_succ (" +
                "id,courierName,param,execTime,execServerIP,execBgnTime,execEndTime,execMillis,failTimes,failMessage" +
                ") SELECT id,courierName,param,execTime,execServerIP,?,?,?,failTimes,failMessage " +
                "FROM courier_task_info WHERE id=?";
    }

    @Override
    public String DELETE_TASK_INFO() {
        return "DELETE FROM courier_task_info WHERE id=?";
    }

    @Override
    public String RECORD_TASK_LOAD() {
        return "UPDATE courier_task_info SET execServerIP=?,loadFlag=1,loadTime=now() WHERE id=?";
    }

    @Override
    public String LIST_TASK_INFO_4EXEC() {
        return "SELECT id,courierName,param,execTime,failTimes FROM courier_task_info WHERE loadFlag=0 " +
                "AND execTime<=? AND (execEndTime IS NULL OR execEndTime<=?) AND MOD(id,?)=? LIMIT 25 " +
                "UNION ALL " +
                "SELECT id,courierName,param,execTime,failTimes FROM courier_task_info WHERE loadFlag=1 " +
                "AND execTime<=now() AND loadTime<=? AND MOD(id,?)=? LIMIT 25";
    }

    @Override
    public String UNLOAD_TASKS() {
        return "UPDATE courier_task_info SET loadFlag=0 WHERE loadFlag=1 AND execServerIP=?";
    }

    @Override
    public String COUNT_TASK_INFO() {
        return "SELECT COUNT(*) FROM courier_task_info";
    }

    @Override
    public String COUNT_TASK_INFO_SUCC() {
        return "SELECT COUNT(*) FROM courier_task_info_succ";
    }

    @Override
    public String PAGE_TASK_INFO() {
        return "SELECT id,courierName,LEFT(param, 20) AS param,execTime,execServerIP,execEndTime,execMillis,failTimes,LEFT(failMessage, 20) AS failMessage,delFlag " +
                "FROM courier_task_info ORDER BY execTime LIMIT ?,?";
    }

    @Override
    public String PAGE_TASK_INFO_SUCC() {
        return "SELECT id,courierName,LEFT(param, 20) AS param,execTime,execServerIP,execEndTime,execMillis,failTimes,LEFT(failMessage, 20) AS failMessage,delFlag " +
                "FROM courier_task_info_succ ORDER BY execEndTime DESC LIMIT ?,?";
    }

    @Override
    public String GET_FAIL_MESSAGE() {
        return "SELECT failMessage FROM courier_task_info WHERE id=?";
    }

    @Override
    public String GET_FAIL_MESSAGE_SUCC() {
        return "SELECT failMessage FROM courier_task_info_succ WHERE id=?";
    }

    @Override
    public String GET_TASK_INFOS_4EXEC(List<Long> ids) {
        return "SELECT id,courierName,param,execTime,failTimes FROM courier_task_info " +
                "WHERE id IN (" + CourierUtil.toString(ids) + ")";
    }

    @Override
    public String GET_PARAM() {
        return "SELECT param FROM courier_task_info WHERE id=?";
    }

    @Override
    public String GET_PARAM_SUCC() {
        return "SELECT param FROM courier_task_info_succ WHERE id=?";
    }

    @Override
    public String UPDATE_PARAM() {
        return "UPDATE courier_task_info SET param=? WHERE id=?";
    }

    @Override
    public String DELETE_SUCC() {
        return "DELETE FROM courier_task_info_succ WHERE execEndTime < ? LIMIT 500";
    }

}
